package com.rc.evcharger.service.discount;

import com.rc.evcharger.common.constant.CommonConstant;
import com.rc.evcharger.common.constant.WebConstant;
import com.rc.evcharger.common.utils.DateHelper;
import com.rc.evcharger.mapper.discount.TenantDiscountMemberStationMapper;
import com.rc.evcharger.model.discount.TenantDiscountMemberStation;
import com.rc.evcharger.model.discount.TenantDiscountPolicy;
import com.rc.evcharger.model.discount.TenantDiscountTime;
import com.rc.evcharger.model.order.OrderDiscountRecord;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.service.order.OrderDiscountRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * Create by Yang on 2019/11/25
 */
@Service
public class TenantDiscountMemberStationServiceImpl implements TenantDiscountMemberStationService {

    @Autowired
    private TenantDiscountMemberStationMapper tenantDiscountMemberStationMapper;

    @Autowired
    private TenantDiscountPolicyService tenantDiscountPolicyService;

    @Autowired
    private TenantDiscountTimeService tenantDiscountTimeService;

    @Autowired
    private OrderDiscountRecordService orderDiscountRecordService;

    @Override
    public TenantDiscountMemberStationMapper getMapper() {
        return tenantDiscountMemberStationMapper;
    }

    /**
     * 通过桩群、会员得到有效优惠方案的时段
     *
     * @param memberId
     * @param stationId
     * @param policyDiscountType 0服务费打折 1 特定服务费（活动价） 2总价打折 默认0
     * @return
     */
    @Override
    public TenantDiscountTime selectByMemberIdAndEsId(Long memberId, Long stationId, String policyDiscountType) {

        //针对全体用户的优惠方案
        TenantDiscountPolicy tenantDiscountPolicyTotal = tenantDiscountPolicyService.selectTotalMember(stationId, new Date(), policyDiscountType);
        if (tenantDiscountPolicyTotal != null) {
            //得到有效方案的时段
            String time = DateHelper.dataToString(new Date(), DateHelper.DATE_FORMAT_HHMMSS);
            return tenantDiscountTimeService.selectValidTime(tenantDiscountPolicyTotal.getDiscountPolicyId(), time);
        }


        //得到该会员的优惠方案
        List<TenantDiscountMemberStation> tenantDiscountMemberStations = tenantDiscountMemberStationMapper.selectByMemberIdAndEsId(memberId, stationId);
        if (CollectionUtils.isEmpty(tenantDiscountMemberStations)) {
            return null;
        }

        for (TenantDiscountMemberStation tdms : tenantDiscountMemberStations) {

            //有效方案,有效时间的时分秒由后台设置。我们只取当前日期就行
            TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyService.selectValidDPolicy(tdms.getDiscountPolicyId(), new Date(), policyDiscountType);
            if (tenantDiscountPolicy != null) {
                //得到有效方案的时段
                String time = DateHelper.dataToString(new Date(), DateHelper.DATE_FORMAT_HHMMSS);
                return tenantDiscountTimeService.selectValidTime(tdms.getDiscountPolicyId(), time);
            }

        }
        return null;
    }

    @Override
    public TenantDiscountTime selectByMemberIdAndEsId(Long memberId, Long stationId) {
        return null;
    }

    //通过方案ID、站点、会员ID查找指定用户的会员
    @Override
    public TenantDiscountTime selectByMemberIdAndEsId(Long memberId, Long stationId, Long discountPolicyId) {
        TenantDiscountMemberStation tenantDiscountMemberStation = tenantDiscountMemberStationMapper.selectByMemberIdAndEsIdAndPolicyId(memberId, stationId, discountPolicyId);
        if (tenantDiscountMemberStation != null) {
            //得到有效方案的时段
            String time = DateHelper.dataToString(new Date(), DateHelper.DATE_COMMON_FORMAT);
            return tenantDiscountTimeService.selectValidTime(discountPolicyId, time);
        }
        return null;

    }

    /**
     * 失效
     *
     * @param dmsId
     */
    @Override
    public void updateStatusInvalid(Long dmsId) {

        TenantDiscountMemberStation tenantDiscountMemberStation = tenantDiscountMemberStationMapper.selectByPrimaryKey(dmsId);
        //失效
        tenantDiscountMemberStation.setStatus(WebConstant.CON1 + "");
        tenantDiscountMemberStationMapper.updateByPrimaryKeySelective(tenantDiscountMemberStation);
    }

    @Override
    public void updateSaleServiceAmountByOrder(OrderInfo orderInfo) {
        TenantDiscountTime tenantDiscountTime = this.selectByMemberIdAndEsId(orderInfo.getMemberId(), orderInfo.getElecStationId(), CommonConstant.POLICY_DISCOUNT_TYPE_SERVICE);
        if (tenantDiscountTime == null) {
            return;
        }

        //服务费折扣
        orderInfo.setDiscountServiceAmount(orderInfo.getTotalSeviceMoney().multiply(tenantDiscountTime.getDiscount()).setScale(2, RoundingMode.HALF_UP));

        //累加被折扣金额
        BigDecimal subtract = orderInfo.getTotalSeviceMoney().subtract(orderInfo.getDiscountServiceAmount());
        orderInfo.setDiscountAmount(orderInfo.getDiscountAmount().add(subtract));

        orderInfo.setPayAmount(orderInfo.getTotalMoney().subtract(orderInfo.getDiscountAmount()));

        //添加优惠明细表
        TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyService.selectByPrimaryKey(tenantDiscountTime.getDiscountPolicyId());
        OrderDiscountRecord orderDiscountRecord = new OrderDiscountRecord();
        orderDiscountRecord.setCreatedTime(new Date());
        orderDiscountRecord.setTenantCode(orderInfo.getTenantCode());
        orderDiscountRecord.setOrderId(orderInfo.getOrderId());

        orderDiscountRecord.setDiscountAmount(subtract);
        orderDiscountRecord.setDiscountName(tenantDiscountPolicy.getPolicyName());
        orderDiscountRecord.setDiscountType("0"); //服务费折扣
        orderDiscountRecordService.insertSelective(orderDiscountRecord);
    }


}
